<?php

namespace App\Http\Resources\OpenShop\ShopOrder;

use App\Http\Resources\BaseResource;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Support\ChangeSupport;
use Illuminate\Http\Request;

class OrderLogResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'id'           => $this->id,
            'content'      => (string)$this->content,
            'orderId'      => $this->orderId,
            'type'         => $this->type,
            'operatorName' => ($this->type == OpenShopSignOrderLogModel::INVALID_ORDER && empty($this->operatorName)) ? "系统操作" : $this->operatorName,
            'changes'      => $this->getChangeDetail(),
            'createdAt'    => $this->createdAt->format('Y-m-d H:i'),
        ];
    }

    /**
     * @return array|mixed
     */
    public function getChangeDetail()
    {
        if(($changeContents = json_decode($this->changes, true))){
            return collect($changeContents)->map(function($changeAll){
                $changeAll["data"] = collect($changeAll["data"])->map(function($change){
                    $change = (object)$change;
                    switch ($change->type){
                        case ChangeSupport::FILED_TYPE_PRIVATE_PICTURE:
                            if(is_array($change->afterText)){
                                $change->afterText = collect($change->afterText)->map("privateFilePath")->toArray();
                            }else{
                                $change->afterText = privateFilePath($change->afterText);
                            }

                            if(is_array($change->beforeText)){
                                $change->beforeText = collect($change->beforeText)->map("privateFilePath")->toArray();
                            }else{
                                $change->beforeText = privateFilePath($change->beforeText);
                            }
                            break;
                    }
                    return (object)$change;
                })->toArray();
                return $changeAll;
            })->toArray();
        }
        return $this->changes;
    }
}
